import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/helper/app_update.dart';
import 'package:kxy_flutter_app/view/root/login_view.dart';
import 'package:kxy_flutter_app/view/root/tab_view.dart';
import 'package:kxy_flutter_app/view/root/view_model/auth_model.dart';
import 'package:provider/provider.dart';

class RootView extends StatefulWidget {
  RootView({Key key}) : super(key: key);

  @override
  _RootViewState createState() => _RootViewState();
}

class _RootViewState extends State<RootView> {
  @override
  void initState() {
    super.initState();
    var appUpdate = new AppUpdate();
    appUpdate.checkUpdate(context, withLatestVersionTip: false);
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<AuthModel>(
      builder: (context, authModel, child) {
        return FutureBuilder(
          future: authModel.authorizeFromCache(),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            return authModel.isAuthed ? TabView() : LoginView();
          },
        );
      },
    );
  }
}
